package com.example.beryl.proSystem;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;
import com.example.beryl.R;
import com.example.beryl.Util.HandlePublic;
import com.example.beryl.Util.ReceiveBroadCast;
import com.example.beryl.entity.Action;
import com.example.beryl.entity.PReport.FinalProcess;
import com.example.beryl.entity.PReport.StartWork;
import com.example.beryl.entity.UserData.EQ;
import com.example.beryl.entity.UserData.Person;
import com.example.beryl.entity.PReport.ProductRE;
import com.example.beryl.http.Net_productReport;
import com.example.beryl.http.Net_userData;
import com.google.gson.Gson;
import com.manu.mdatepicker.FontType;
import com.manu.mdatepicker.MDatePicker;
import com.example.beryl.databinding.ActivityProductionReportingBinding;
import org.json.JSONArray;
import org.json.JSONException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class productionReport extends AppCompatActivity {
    ActivityProductionReportingBinding binding;
    private static final int PR = 1;//流传单回调
    private static final int BC = 1;//下拉班次
    private static final int FGBG = 2;//下拉返工报工
    private static final int EQ = 2;
    private static final int PM = 3;
    private static final int Query = 4;
    private static final int checkPRData = 5;
    private static final int SummitData = 6;
    private static final int TimeBC = 7;
    private static final int UnProductD = 8;
    private static Dialog dialogRec = null;
    ProductRE productRE = new ProductRE();
    FinalProcess finalProcess = new FinalProcess();
    StartWork startWork = new StartWork();
    public ConstraintLayout clMain;
    public boolean mReceiverTag = false;//广播注册标志
    ReceiveBroadCast mBroadcastReceiver = new ReceiveBroadCast();
    HandlePublic handlePublic = new HandlePublic();
    Net_userData net_userData = new Net_userData();
    Net_productReport net_productReport = new Net_productReport();
    ArrayAdapter<String> adapter = null;
    ArrayAdapter<String> adapter1 = null;
    ArrayAdapter<String> adapter2 = null;
    private  String BCSelect = "";
    private  String FGBGSelect = "";
    private  String GXSelect = "";
    private String jsonGX = null; //工序json
    private String lastGX = null;//最后工序
    private String finalJson = "";//提交json



    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        mReceiverTag = true;
        filter.addAction("com.android.server.scannerservice.broadcast"); //添加过滤条件，接收广播
        registerReceiver(mBroadcastReceiver, filter);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_production_reporting);
        String date = handlePublic.Date(2);
        binding.btnDateBottom.setText(date.split(" ")[0]);
        binding.btnDateBottom1.setText(date.split(" ")[0]);

        Toolbar toolbar = findViewById(R.id.toolbar);
        clMain = findViewById(R.id.cl_main);
        toolbar.setTitle("生产报工");
        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back);
        toolbar.setNavigationOnClickListener(view -> finish());
        toolbar.setOnMenuItemClickListener(item -> {
            switch (item.getGroupId()) {
            }
            return false;
        });
        List<String> listBC = new ArrayList<>(Arrays.asList("白","夜"));
        List<String> listFGBG = new ArrayList<>(Arrays.asList("否","是"));
        setSpinner(BC,listBC);
        setSpinner(FGBG,listFGBG);
        mBroadcastReceiver.setScanHeadListener(this::getReBroad);

        handlePublic.setGetResult(this::getHandle);

        //班次选择
        binding.dropdown1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                BCSelect = adapter.getItem(i);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        //返工报工选择
        binding.dropdown11.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                FGBGSelect = adapter1.getItem(i);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        binding.dropdown111.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                 GXSelect = adapter2.getItem(i);
             }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        //手输工号
        binding.peopleName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 4)
                {
                    boolean T = handlePublic.isNumericZidai(charSequence.toString());
                    if (T)
                    {
                        String PersonNUm = "EM-" + charSequence.toString();
                        getReBroad(PersonNUm);
                    }
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //手动输入设备
        binding.EQSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!GXSelect.equals(""))
                {
                    String input = binding.EQNum.getText().toString();
                    if (!input.contains("#"))
                    {
                        input = input + "#";
                    }
                    String finalInput = input;
                    binding.EQNum.setText(finalInput);
                    HashMap<String,String> hashMap = new HashMap<>();
                    hashMap.put("设备",finalInput);
                    hashMap.put("参数",GXSelect);
                    String json = handlePublic.MakeJson(hashMap);
                    System.out.println(json);
                   // handlePublic.startLoad(productionReport.this);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            CommSelectMethod(Action.action.GetEM, json,EQ);
                        }
                    }).start();
                }else
                {
                    handlePublic.Alert("请确认好工序",productionReport.this);
                }
            }
        });


        //手动输入流传单号
        binding.selectButtom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String LCDnum = binding.LCD.getText().toString();
                if (!LCDnum.equals(""))
                {
                    if (!LCDnum.contains("WO-"))
                    {
                        LCDnum = "WO-" + LCDnum;
                    }
                    getReBroad(LCDnum);
                }else
                {
                    handlePublic.Alert("请输入流传单号!",productionReport.this);
                }
            }
        });

        //时间控制
        binding.TimeEND.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 4)
                {
                   if (!charSequence.toString().contains(":"))
                   {
                       getBC(charSequence.toString());
                   }
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        binding.submitButton.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(productionReport.this);
            builder.setTitle("提示");
            builder.setMessage("是否提交并检查数据完整");
            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    checkData();
                }
            });
            builder.setNegativeButton("否",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            builder.create().show();
        });

        //更新操作员
        handlePublic.setGetPersonRet(new HandlePublic.getPersonInter() {
            @Override
            public void getPersonIn(String ret) {
                InsertPeople(ret);
            }
        });

        //不良品详细按钮
        binding.UnProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UnProductData();
            }
        });
    }

    //根据时间更新班次
    private void getBC(String time) {
        StringBuilder sb = new StringBuilder(time);
        sb.insert(2, ':');
        binding.TimeEND.setText(sb);
        //handlePublic.startLoad(productionReport.this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                CommSelectMethod(Action.action.getTimeBC,time,TimeBC);
            }
        }).start();
    }

    public void btnClickDateBottom(View view) {
        MDatePicker.create(this)
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.BOTTOM)
                .setOnlyYearMonth(false)
                .setTwelveHour(true)
                .setFontType(FontType.LARGE)
                .setOnDateResultListener(date -> {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(date);
                    SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                    dateFormat.applyPattern("yyyy/MM/dd");
                    binding.btnDateBottom.setText(dateFormat.format(new Date(date)));
                    //Toast.makeText(Production_reporting.this, dateFormat.format(new Date(date)), Toast.LENGTH_SHORT).show();
                })
                .build()
                .show();
    }

    public void btnClickDateBottom1(View view) {
        MDatePicker.create(this)
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.BOTTOM)
                .setOnlyYearMonth(false)
                .setTwelveHour(true)
                .setFontType(FontType.LARGE)
                .setOnDateResultListener(date -> {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(date);
                    SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                    dateFormat.applyPattern("yyyy/MM/dd");
                    binding.btnDateBottom1.setText(dateFormat.format(new Date(date)));
                    //Toast.makeText(ProductionReporting.this, dateFormat.format(new Date(date)), Toast.LENGTH_SHORT).show();
                })
                .build()
                .show();
    }

    //广播数据回调
    private void getReBroad(String para)
    {
        View view = getCurrentFocus();
        //view.clearFocus();
        handlePublic.getT(view,productionReport.this);
        boolean Act = isFinishing();
        //判断是否完成绘制
        if (Act)
        {
            return;
        }

        switch(para.substring(0,2))
        {
            case "WO"://流传单
                binding.LCD.setText(para);
                //handlePublic.startLoad(productionReport.this);
                new Thread(() -> CommSelectMethod(Action.action.getProductReport,para,PR)).start();
                break;
            case "EQ":
            case "EM":
                String w = para.split("-")[0];
                if (w.equals("EQ"))
                {
                    if (GXSelect.equals(""))
                    {
                        handlePublic.Alert("请先扫描流传单",this);
                        return;
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(productionReport.this);
                    builder.setTitle("提示");
                    builder.setMessage("确认好当前选择工序是否正确?");
                    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            HashMap<String,String> hashMap = new HashMap<>();
                            hashMap.put("设备",para);
                            hashMap.put("参数",GXSelect);
                            String json = handlePublic.MakeJson(hashMap);
                            //handlePublic.startLoad(productionReport.this);
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    CommSelectMethod(Action.action.GetEM,json,EQ);
                                }
                            }).start();
                        }
                    });
                    builder.setNegativeButton("否",
                            (dialog, which) -> dialog.dismiss());
                    if(!isFinishing()) {
                        builder.create().show();
                    }
                }else
                {
                    //handlePublic.startLoad(productionReport.this);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            CommSelectMethod(Action.action.GetPerson,para.split("-")[1],PM);
                        }
                    }).start();
                }
                break;
            }
    }

    //数据库数据回调
    private void getHandle(int what, Object t) throws JSONException {
        List listgetData;
        Gson gson = new Gson();
        listgetData= (List)t;
        String getlist = listgetData.get(0).toString();
        if (getlist.contains("[") || getlist.contains("{"))
        {
            switch (what)
            {
                case PR://流传单回调
                    getlist = handlePublic.JosnChange(getlist);
                    productRE = gson.fromJson(getlist,ProductRE.class);//JSON转为实体类
                    binding.PCNUM.setText(productRE.cMOLotNo);
                    //binding.PCNUM.clearFocus();//清除焦点
                    HashMap<String,String> list11 = new HashMap<>();
                    list11.put("cMOLotNo",productRE.cMOLotNo);
                    list11.put("cMatNo",productRE.cMatNo);
                    list11.put("dLotQty",productRE.dLotQty);
                    String jsonArrayList = gson.toJson(list11);
                    jsonArrayList = handlePublic.JosnChange(jsonArrayList);
                    String finalJsonArrayList = jsonArrayList;
                    System.out.println(finalJsonArrayList);
                    //handlePublic.startLoad(productionReport.this);
                    new Thread(() -> {
                        CommSelectMethod(Action.action.getProductReportQuery,finalJsonArrayList,Query);//得到生产批次号后查询
                    }).start();
                    break;
                case PM://作业员结果回调
                    selectPM(getlist);
                    break;
                case EQ://设备结果回调
                    com.example.beryl.entity.UserData.EQ eq = gson.fromJson(getlist,EQ.class);
                    binding.EQNum.setText(eq.cSuperviseNo);
                    break;
                case Query://查询结果回调
                    JSONArray jsonArrayQuery = new JSONArray(getlist);
                    QueryData(jsonArrayQuery);
                    break;
//                    case checkPRData://提交数据检查回调
//                        JSONArray jsonCheckData = new JSONArray(getlist);
//                        CheckDataDeal(jsonCheckData);
//                        break;
                case SummitData:
                    CleanForm();
                    handlePublic.Alert("提交成功",this);
                    break;
                case TimeBC:
                    getlist = handlePublic.ChangeString(getlist);
                    if (getlist.contains("白"))
                    {
                        setSpinner(BC,new ArrayList<>(Arrays.asList("白","夜")));
                    }else
                    {
                        setSpinner(BC,new ArrayList<>(Arrays.asList("夜","白")));
                    }
                    break;
                case UnProductD:
                    //如果dialog不为null
                    if (dialogRec != null)
                    {
                        dialogRec.dismiss(); //取消弹窗
                        handlePublic.Alert("不良品与良品提交成功",this);
                        CleanForm();
                    }
                    break;
            }
        }else
        {
            //如果设备不存在是 清除单元格
            if (getlist.contains("设备维护或不存在"))
            {
                binding.EQNum.setText("");
            }
            handlePublic.Alert(getlist,this);
        }
    }


    /**
     * 查询方法
     * @param jsonArrayQuery
     * @throws JSONException
     */
    private void QueryData(JSONArray jsonArrayQuery) throws JSONException {
            Gson gson = new Gson();
            String 工序 = jsonArrayQuery.getJSONObject(0).get("工序").toString();
            //jsonGX = 工序;
            //String 最后工序 = jsonArrayQuery.getJSONObject(0).get("最后工序").toString();
            //lastGX = 最后工序;
            String 已开工 = jsonArrayQuery.getJSONObject(0).get("已开工").toString();
            if (已开工.contains("["))
            {
                已开工 = handlePublic.JosnChange(已开工);
                String 来料数 = jsonArrayQuery.getJSONObject(0).get("来料数").toString();
                String 员工 = jsonArrayQuery.getJSONObject(0).get("员工").toString();
                startWork = gson.fromJson(已开工,StartWork.class);
                finalProcess.setcOperationName(startWork.cProcess);
                finalProcess.setcOperationNo(startWork.cProcessCode);
                //finalProcess = gson.fromJson(最后工序,FinalProcess.class);
                JSONArray jsonArrayGX = new JSONArray(工序);
                List<String> listGX = new ArrayList<>();
                for (int i = 0 ;i < jsonArrayGX.length(); i++)
                {
                    listGX.add(jsonArrayGX.getJSONObject(i).get("cOperationName").toString());
                }
                listGX.remove(finalProcess.cOperationName);
                listGX.add(0,finalProcess.cOperationName);//放回首位
                setSpinner(99,listGX);//else 数字随便写
                if (!员工.contains("员工查询失败"))
                {
                    员工 = handlePublic.JosnChange(员工);
                    Person Ps = gson.fromJson(员工,Person.class);
                    Action.person = Ps;
                    binding.peopleName.setText(Action.person.cPsn_Name);//操作员
                }
                binding.EQNum.setText(startWork.PEquipment);//设备
                String bc = startWork.Bc;
                if (bc.equals("0"))
                {
                    setSpinner(BC,new ArrayList<>(Arrays.asList("白","夜")));
                }else
                {
                    setSpinner(BC,new ArrayList<>(Arrays.asList("夜","白")));
                }
                binding.btnDateBottom.setText(startWork.startDay);//开始日期
                binding.TimeSTArt.setText(startWork.startHM);//开始时间
                binding.TimeEND.setText(startWork.endTime);//结束时间
                binding.LLNUM.setText(来料数);//来料数
            }else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage(已开工);
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            JSONArray jsonArrayGX = new JSONArray(工序);
                            List<String> listGX = new ArrayList<>();
                            for (int j = 0 ;j < jsonArrayGX.length(); j++)
                            {
                                listGX.add(jsonArrayGX.getJSONObject(j).get("cOperationName").toString());
                            }
                            setSpinner(99,listGX);//else 数字随便写 写入工序
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
                builder.setNegativeButton("否",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
    }

    /**
     * 数据库查询通用方法
     * @param action //查询Action
     * @param para // 数据
     * @param what //回调后的参数
     */
    private void CommSelectMethod(String action,String para,int what) {
        List BList;
        if (what == EQ || what == PM)//如果参数为查找人员或设备
        {
            BList = net_userData.User_CommonMethod(action,para);
        }else
        {
            BList = net_productReport.CS_CommonMethod(action,para);
        }
        handlePublic.setmHandler(what,BList);
    }

    //下拉选择框
    private void setSpinner(int i,List<String> datalist)
    {
        Spinner spinner;
        if (i == BC)//班次
        {
            spinner = binding.dropdown1;
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, datalist);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }else if (i == FGBG)//返工报工
        {
            spinner = binding.dropdown11;
            adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, datalist);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter1);
        }else//工序
        {
            spinner = binding.dropdown111;
            adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, datalist);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter2);
        }
    }

    private void checkData() {

        String txt = binding.EQNum.getText().toString();
        if (txt.equals(""))
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(productionReport.this);
            builder.setTitle("提示");
            builder.setMessage("设备信息未被填写,是否继续？");
            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    checkSummitData();
                }
            });
            builder.setNegativeButton("否",
                    (dialog, which) -> dialog.dismiss());
            if (!isFinishing()) {
                builder.create().show();
            }
        }
        else
        {
            checkSummitData();
        }
    }



    //获取不良品按钮需要的数据
    private void UnProductData()
    {
        String json = checkInputData();
        if (json.equals(""))
        {
            handlePublic.Alert("请把表格输入完整",this);
        }else
        {
            InputUnProduct(json);
        }
    }

    //获取不良品表单输入数据
    private void InputUnProduct(String JsonData)
    {
        final Dialog dialog = new Dialog(productionReport.this);
        dialog.setContentView(R.layout.activity_defective_p);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        dialog.setCanceledOnTouchOutside(false);//点击空白不消失
        dialog.setCancelable(false);//返回键不消失
        dialogWindow.setAttributes(lp);
        dialog.show();
        final Button button = dialog.findViewById(R.id.confirm);//确认按钮
        final Button button1 = dialog.findViewById(R.id.back);//返回按钮
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ret = MakeUPData(JsonData,dialog);
                System.out.println(ret);
                dialogRec = dialog;
                //handlePublic.startLoad(productionReport.this);
                new Thread(() -> CommSelectMethod(Action.action.UnProductData,ret,UnProductD)).start();
            }
        });
        button1.setOnClickListener(v -> dialog.dismiss());
    }

    //组建不良品json数据
    private String MakeUPData(String JsonData,Dialog dialog)
    {
        HashMap<String,String> hashMap = new HashMap<>();
        final EditText cl = dialog.findViewById(R.id.CL);
        String cl_text = cl.getText().toString();
        hashMap.put("CL",cl_text);

        final EditText CH = dialog.findViewById(R.id.CH);
        String CH_text = CH.getText().toString();
        hashMap.put("CH",CH_text);

        final EditText DF = dialog.findViewById(R.id.DF);
        String DF_text = DF.getText().toString();
        hashMap.put("DF",DF_text);

        final EditText ESR = dialog.findViewById(R.id.ESR);
        String ESR_text = ESR.getText().toString();
        hashMap.put("ESR",ESR_text);

        final EditText LC = dialog.findViewById(R.id.LC);
        String LC_text = LC.getText().toString();
        hashMap.put("LC",LC_text);

        final EditText Others = dialog.findViewById(R.id.Others);
        String Others_text = Others.getText().toString();
        hashMap.put("Others",Others_text);

        final EditText WorkTime = dialog.findViewById(R.id.WorkTime);
        String WT_text = WorkTime.getText().toString();
        hashMap.put("WorkTime",WT_text);

        final EditText P_time = dialog.findViewById(R.id.P_time);
        String pTime_text = P_time.getText().toString();
        hashMap.put("P_time",pTime_text);

        final EditText maintenance_time = dialog.findViewById(R.id.maintenance_time);
        String mTime_text = maintenance_time.getText().toString();
        hashMap.put("maintenance_time",mTime_text);

        final EditText Change = dialog.findViewById(R.id.Change);
        String Change_time = Change.getText().toString();
        hashMap.put("Change",Change_time);

        final EditText jianyan = dialog.findViewById(R.id.jianyan);
        String jYan_text = jianyan.getText().toString();
        hashMap.put("jianyan",jYan_text);

        final EditText Other_Time = dialog.findViewById(R.id.Other_Time);
        String oTime_text = Other_Time.getText().toString();
        hashMap.put("Other_Time",oTime_text);
        String formJson = handlePublic.MakeJson(hashMap);
        List<String> strings = new ArrayList<>();
        strings.add(formJson);
        strings.add(JsonData);
        Gson gson = new Gson();
        String ret = gson.toJson(strings);
        return ret;
    }



    //检查不良品需要数据是否填好
    private String checkInputData()
    {
        String ret = "";
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("当前工序",GXSelect);
        hashMap.put("生产批次号",binding.PCNUM.getText().toString());
        hashMap.put("员工名称",binding.peopleName.getText().toString());
        if (Action.person == null)
        {
            hashMap.put("员工Num","");
        }else
        {
            hashMap.put("员工Num",Action.person.cPsn_Num);
        }

        hashMap.put("来料数",binding.LLNUM.getText().toString());
        hashMap.put("良品数",binding.ProductNum.getText().toString());
        hashMap.put("不良数",binding.UnProductNum.getText().toString());
        hashMap.put("开始日期",binding.btnDateBottom.getText().toString());
        hashMap.put("结束日期",binding.btnDateBottom1.getText().toString());
        hashMap.put("开始时间",binding.TimeSTArt.getText().toString());
        hashMap.put("结束时间",binding.TimeEND.getText().toString());
        hashMap.put("返工报工",FGBGSelect);
        hashMap.put("班次",BCSelect);
        hashMap.put("订单数量",productRE.dLotQty);
        hashMap.put("生产订单号",productRE.cOrderNo);
        hashMap.put("lotType",productRE.cLotType);
        if(hashMap.containsValue("")) {
            return ret;
        }else
        {
            hashMap.put("设备",binding.EQNum.getText().toString());
            ret = handlePublic.MakeJson(hashMap);
        }
        return ret;
    }



    //数据前检查确认
    private void checkSummitData() {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("生产批次号",binding.PCNUM.getText().toString());
        hashMap.put("员工名称",binding.peopleName.getText().toString());
        hashMap.put("员工Num",Action.person.cPsn_Num);
        hashMap.put("良品数",binding.ProductNum.getText().toString());
        hashMap.put("开始日期",binding.btnDateBottom.getText().toString());
        hashMap.put("结束日期",binding.btnDateBottom1.getText().toString());
        hashMap.put("开始时间",binding.TimeSTArt.getText().toString());
        hashMap.put("结束时间",binding.TimeEND.getText().toString());
        hashMap.put("班次",BCSelect);
        hashMap.put("不良数",binding.UnProductNum.getText().toString());
        hashMap.put("来料数",binding.LLNUM.getText().toString());
        hashMap.put("当前工序",GXSelect);
        //hashMap.put("当前工序No",finalProcess.cOperationNo);
        //hashMap.put("工序",jsonGX);
        // hashMap.put("最后工序",lastGX);
        hashMap.put("订单数量",productRE.dLotQty);
        //hashMap.put("部门编码",productRE.cProductDepNo);
        hashMap.put("物料规格",productRE.cSpecifucation);
        hashMap.put("返工报工",FGBGSelect);
        hashMap.put("物料编码",productRE.cMatNo);
        hashMap.put("生产订单号",productRE.cOrderNo);

        //设备提示不检查
        if(hashMap.containsValue("")) {
            handlePublic.Alert("请把表格输入完整",this);
        }else
        {
            hashMap.put("设备",binding.EQNum.getText().toString());
            finalJson = handlePublic.MakeJson(hashMap);
            System.out.println(finalJson);
            //handlePublic.startLoad(productionReport.this);
            new Thread(() -> CommSelectMethod(Action.action.PRSummitData, finalJson,SummitData)).start();
        }
    }

    @Override
    public void onBackPressed() {
        if (mReceiverTag) {
            unregisterReceiver(mBroadcastReceiver);
            finish();
        }
    }

    //更新操作员
    //选择操作员
    private void  selectPM(String ret) throws JSONException {
        String rets = "";
        rets = handlePublic.JosnChange(ret);
        JSONArray jsonret = new JSONArray(rets);
        //如果返回数据大于1
        if (jsonret.length() > 1)
        {
            handlePublic.AlterDialog(productionReport.this,jsonret);
        }
        else
        {
            //返回一条数据时
            InsertPeople(ret);
        }
    }

    //更新操作员
    private void InsertPeople(String para)
    {
        Gson gson = new Gson();
        Person EM = gson.fromJson(para, Person.class);
        Action.person = EM;
        System.out.println(Action.person.cPsn_Num);
        binding.peopleName.setText(Action.person.cPsn_Name);
    }

    //清空表单
    private void CleanForm()
    {
        binding.LCD.setText("");
        binding.PCNUM.setText("");
        binding.EQNum.setText("");
        binding.ProductNum.setText("");
        binding.TimeSTArt.setText("");
        binding.TimeEND.setText("");
        binding.UnProductNum.setText("0");
        binding.LLNUM.setText("0");
        finalJson = "";
    }

}